﻿using CGT.GOP.Common;
using CGT.Infrastructure.Entity;
using CGT.Infrastructure.Service;
using OF.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace CGT.GOP.App_Start
{
    public class AuthenticationAttribute: ActionFilterAttribute
    {
        GovMenuService menuService = new GovMenuService();
        GovUserService userService = new GovUserService();
        /// <summary>
        /// 菜单编码
        /// </summary>
        public int MenuCode { set; get; }

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            UserInfo usr = FormsPrincipal.IsAuthenticated();
            if (usr == null || usr.UserId == 0)
            {
                filterContext.HttpContext.Response.Redirect("~/500.html");//否则跳转出错页面
            }

            GovUser userObj = userService.Single(usr.UserId);
            if (userObj == null)
            {
                filterContext.HttpContext.Response.Redirect("~/500.html");//否则跳转出错页面
            }

            GovMenu menuObj = menuService.GetUserMenuByRoleIdMenuCode(userObj.RoleId, MenuCode);
            if (menuObj == null)
            {
                filterContext.HttpContext.Response.Redirect("~/403.html");//否则跳转无权限页面
            }

            if (userObj.IsPolice == 0 && menuObj.OnlyForPolice == 1)
            {
                filterContext.HttpContext.Response.Redirect("~/403.html");//否则跳转无权限页面
            }

            base.OnActionExecuting(filterContext);
          
        }
    }
}